/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javafx.scene.CustomNode;
import vinciappli.ViewInterface;
import java.util.Timer;
import java.util.TimerTask;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import vinciappli.Templates;
import control.ControllerPrincipal;

/**
 * @author Sik
 */
 
 
 var INSTANCE : ViewPrincipal;
 
public class ViewPrincipal extends CustomNode, ViewInterface
{
    var screens : CustomNode[];
    
    def startScreen : StartScreen =
    StartScreen
    {
        visible: true
        onKeyPressed: function(e: KeyEvent): Void
         {
                if(e.code == KeyCode.VK_ENTER)
                {
                    startScreen.close();
                    nextScreen(startScreen);
                }
         }
    }

    def menuScreen : MenuScreen =
    MenuScreen
    {
        visible: false;
    }

    def playerScreen : PlayerScreen =
    PlayerScreen
    {
        visible: false;
    }

    var gameScreen : GameScreen =
    GameScreen
    {
        visible: false;
    }

    override public function build(): Void
    {
        println("Build ViewPrincipal");
        INSTANCE = this;
        
        insert [startScreen, menuScreen, playerScreen, gameScreen] into screens;
        Stage
            {
                title: "Vinci"
                width: Templates.STAGE_WIDTH
                height: Templates.STAGE_HEIGHT
//                resizable: false
                
                scene: Scene
                {
                    content:
                    [
                        Group
                        {
                            content:
                            [
                                startScreen,
                                menuScreen,
                                playerScreen,
                                gameScreen
                            ]
                        }
                    ]
                }
            };
            startScreen.requestFocus();
    }

    public function buildGameBoard() : Void
    {
        INSTANCE.gameScreen.iniGame();
        INSTANCE.gameScreen.visible = true;
    }

    public function resetGameBoard() : Void
    {
        gameScreen = GameScreen
        {
            visible: false;
        };
        ControllerPrincipal.getINSTANCE().resetGame();
    }


    public function getGameScreen() : GameScreen
    {
        return INSTANCE.gameScreen;
    }

    public function getMenuScreen() : MenuScreen
    {
        return INSTANCE.menuScreen;
    }

    public function getScreens() : CustomNode[]
    {
        return INSTANCE.screens;
    }

}

    public function nextScreen(n : Node) : Void
    {
        var isNext = false;

        if(n == ViewPrincipal.getInstance().getGameScreen())
        {
            ViewPrincipal.getInstance().resetGameBoard();ViewPrincipal.getInstance().getMenuScreen().visible = true;
            ViewPrincipal.getInstance().getMenuScreen().requestFocus();
            ViewPrincipal.getInstance().resetGameBoard();
        }
        
        for(s in ViewPrincipal.getInstance().screens)
        {
            if(isNext == true)
            {
                s.visible = true;
                s.requestFocus();
                return;
            }
            if(s == n)
            {
                
                n.visible = false;
                isNext = true;
            }
        }
    }

    public function previousScreen(n : Node) : Void
    {
        var previous : Node;

        for(s in ViewPrincipal.getInstance().screens)
        {
            if(s == n)
            {
                n.visible = false;
                previous.visible = true;
            }
            previous = s;
        }
    }
    
    public function getInstance():ViewPrincipal 
    {
          if (INSTANCE == null) 
          {
                INSTANCE = ViewPrincipal {};
          }
          return INSTANCE;
    }


    

